package icu.qx.pro.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.domain.param.ProWorkOrderParam;
import icu.qx.pro.feign.GbsBomMaterialFeign;
import icu.qx.pro.feign.PbsProuteProcedureFeign;
import icu.qx.pro.service.IProScheduleService;
import icu.qx.pro.service.IProWorkTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.service.IProWorkOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产工单Controller
 *
 * @author yong
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/pro/workOrder")
public class ProWorkOrderController extends BaseController {
    @Autowired
    private IProWorkOrderService proWorkOrderService;
    @Autowired
    private IProScheduleService proScheduleService;
    @Autowired
    private IProWorkTaskService proWorkTaskService;
    @Autowired
    private GbsBomMaterialFeign gbsBomMaterialFeign;
    @Autowired
    private PbsProuteProcedureFeign pbsProuteProcedureFeign;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询生产工单列表
     */
    @RequiresPermissions("pro:workOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(ProWorkOrder proWorkOrder) {
        startPage();
        List<ProWorkOrder> list = proWorkOrderService.selectProWorkOrderList(proWorkOrder);
        list.forEach(e -> {
            List<ProWorkTask> proWorkTasks = proWorkTaskService.selectTaskListByOrderCode(e.getWorkOrderCode());
            e.setProWorkTaskList(proWorkTasks);
        });
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @RequiresPermissions("pro:workOrder:export")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProWorkOrder proWorkOrder) {
        List<ProWorkOrder> list = proWorkOrderService.selectProWorkOrderList(proWorkOrder);
        ExcelUtil<ProWorkOrder> util = new ExcelUtil<ProWorkOrder>(ProWorkOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @RequiresPermissions("pro:workOrder:query")
    @GetMapping(value = "/{workOrderCode}")
    public AjaxResult getInfo(@PathVariable("workOrderCode") String workOrderCode) {
        ProWorkOrder proWorkOrder = proWorkOrderService.selectProWorkOrderByWorkOrderCode(workOrderCode);
        proWorkOrder.setProSchedule(proScheduleService.selectSimpleProScheduleByScheduleCode(proWorkOrder.getScheduleCode()));
        AjaxResult ajaxResult = success(proWorkOrder);
        ajaxResult.put("gbsBomMaterials", gbsBomMaterialFeign.list(proWorkOrder.getWorkOrderBom(), proWorkOrder.getWorkOrderBVersion()).get("data"));
        ajaxResult.put("pbsProcedureRoutes", pbsProuteProcedureFeign.noGoodList(proWorkOrder.getWorkOrderRoute()).get("data"));
        return ajaxResult;
    }

    /**
     * 新增生产工单
     */
    @RequiresPermissions("pro:workOrder:add")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProWorkOrder proWorkOrder) {
        return toAjax(proWorkOrderService.insertProWorkOrder(proWorkOrder));
    }

    /**
     * 工单排产
     */
    @RequiresPermissions("pro:workOrder:edit")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult arrangeWork(@RequestBody ProWorkOrderParam ProWorkOrderParam) {
        List<ProWorkTask> proWorkTasks = ProWorkOrderParam.getProWorkTasks();
        proWorkTasks.forEach(e -> {
            String code = codeUtils.generateCode(TableNameEnum.PRO_WORK_TASK.getTableName());
            e.setWtaskCode(code);
        });
        return success(proWorkOrderService.arrangeWork(ProWorkOrderParam.getProWorkOrder(), proWorkTasks));
    }

    /**
     * 删除生产工单
     */
    @RequiresPermissions("pro:workOrder:remove")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderCodes}")
    public AjaxResult remove(@PathVariable List<String> workOrderCodes) {
        return toAjax(proWorkOrderService.deleteProWorkOrderByWorkOrderCodes(workOrderCodes));
    }
}
